package com.myopen.controller.admin;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.ActionKey;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseDataTableVo;
import com.myopen.base.BaseVo;
import com.myopen.model.WebsiteColumn;
import com.myopen.model.WebsiteContent;

public class WebsiteContentController extends BaseController{

	public void index() {
		render("/admin/websiteContent/index.html");
	}
	
	@ActionKey("/admin/websiteContent/pagequery")
	public void pagequery(){
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	
	@ActionKey("/admin/myWebsiteContent")
	public void myCmsWebsiteContent() {
		render("/admin/myWebsite/myWebsiteContent/index.html");
	}
	
	@ActionKey("/admin/myWebsiteContent/pagequery")
	public void myCmsWebsiteContentPageQuery() {
		
		Record website=getMyWebsite();
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		record.set("module", getPara("module"));
		record.set("websiteId", website.getStr("id"));
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@ActionKey("/admin/myWebsiteContent/recycle")
	public void myCmsWebsiteContentRecycle() {
		render("/admin/myWebsite/myWebsiteContentRecycle/index.html");
	}
	
	@ActionKey("/admin/myWebsiteContent/recycle/pagequery")
	public void myCmsWebsiteContentPageRecycleQuery() {
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		record.set("removed", "1");
		record.set("module", getPara("module"));
		Page<Record> page=WebsiteContent.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ActionKey("/admin/myWebsite/publish")
	public void myCmsPublish(){
		
		//Record currUser=getAdminUser();
		Record websiteColumn=new Record();
		Record websiteContent=new Record();
		Map<String,Object> attribute_map=new HashMap();
		String module=null;
		String websiteColumnId=getPara("websiteColumnId");
		String websiteContentId=getPara("websiteContentId");
		
		if(!StrKit.isBlank(websiteColumnId)) {
			websiteColumn= WebsiteColumn.dao.getRecordById(websiteColumnId);
			module=websiteColumn.getStr("module");
		}
		
		if(!StrKit.isBlank(websiteContentId)) {
			websiteContent=WebsiteContent.dao.getRecordById(websiteContentId);
			attribute_map=websiteContent.get("attribute_map");
			//从详情过来的，可能不带该参数
			if(module==null) {
				websiteColumn= WebsiteColumn.dao.getRecordById(websiteContent.getStr("websiteColumnId"));
				websiteColumnId=websiteColumn.getStr("id");
				module=websiteColumn.getStr("module");
			}
		}
		
		setAttr("websiteColumnId", websiteColumnId);
		setAttr("module", module);
		setAttr("vo", websiteContent);
		setAttr("attribute_map", attribute_map);
		if(module==null) {
			render("/admin/myWebsite/myWebsitePublish/index.html");
		}else {
			render("/admin/myWebsite/myWebsitePublish/"+module+".html");
		}
	}
	
	@ActionKey("/admin/myWebsite/publish/save")
	public void myCmsPublishSave(){
		
		BaseVo baseVo=new BaseVo();
		
		Record currUser=getAdminUser();
		
		String websiteColumnId=getPara("websiteColumnId");
		String title=getPara("title");
		String id=getPara("id");
		Integer px=getParaToInt("px");
		String attribute=getPara("attribute");
		Record websiteColumn=WebsiteColumn.dao.getRecordById(websiteColumnId);
		
		WebsiteContent vo=new WebsiteContent();
		vo.set("websiteId", websiteColumn.getStr("websiteId"));
		vo.set("websiteColumnId", websiteColumnId);
		vo.set("module", websiteColumn.getStr("module"));
		vo.set("title", title);
		vo.set("px", px);
		vo.set("id", id);
		vo.set("attribute", attribute);
		
		WebsiteContent.dao.save(vo, currUser);
		
		baseVo.setCode("0");
		
		renderJson(baseVo);
	}
	
	@ActionKey("/admin/myWebsite/myWebsiteContent/startUsing")
	public void startUsing(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.changeStatus(id, "CMS_CONTENT_STATUS_00", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/myWebsite/myWebsiteContent/forbidden")
	public void forbidden(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.changeStatus(id, "CMS_CONTENT_STATUS_01", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/myWebsite/myWebsiteContent/delete")
	public void delete(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.remove(id,currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/myWebsite/myWebsiteContent/recover")
	public void recover(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		WebsiteContent.dao.recover(id,currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	
}
